const path = require('path')
const webpack = require('webpack')
// 代码压缩
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
// gzip压缩
const CompressionWebpackPlugin = require('compression-webpack-plugin')
// 是否为生产环境
const isProduction = process.env.NODE_ENV !== 'development'
module.exports = {
    // 基本路径
    publicPath: process.env.NODE_ENV === "production" ? "/mobile/" : "/",
    // 输出文件目录
    outputDir: 'dist',
    assetsDir: 'static',
    devServer: {
        port: '8083',
        hot: true
    },
    // webpack配置
    chainWebpack: (config) => {
        // config.resolve.alias
        //     .set('mui', path.resolve(__dirname, './src/assets/js/mui.min.js'))
    },
    configureWebpack: config => {
        config.externals = {};
        // 生产环境相关配置
        if (isProduction) {

            // 公共代码抽离
            config.optimization = {
                splitChunks: {
                    cacheGroups: {
                        vendor: {
                            chunks: 'all',
                            test: /node_modules/,
                            name: 'vendor',
                            minChunks: 1,
                            maxInitialRequests: 5,
                            minSize: 0,
                            priority: 100
                        },
                        common: {
                            chunks: 'all',
                            test: /[\\/]src[\\/]js[\\/]/,
                            name: 'common',
                            minChunks: 2,
                            maxInitialRequests: 5,
                            minSize: 0,
                            priority: 60
                        },
                        styles: {
                            name: 'styles',
                            test: /\.(sa|sc|c)ss$/,
                            chunks: 'all',
                            enforce: true
                        },
                        runtimeChunk: {
                            name: 'manifest'
                        }
                    }
                }
            }
        };
        // config.plugins = [
        //     new webpack.ProvidePlugin({
        //         mui: "mui",
        //         "window.mui": "mui"
        //     })
        // ]
    },
    // vue-loader 配置项
    // https://vue-loader.vuejs.org/en/options.html
    // vueLoader: {},
    // 生产环境是否生成 sourceMap 文件
    productionSourceMap: false,
    // 第三方插件配置
    pluginOptions: {
        // ...
    }
}